package com.wdcg.service;

import java.util.List;

import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.LocationClient;
import com.wdcg.R;
import com.wdcg.activity.MainActivity;
import com.wdcg.model.access.DwarfAccess;
import com.wdcg.utils.GameLogic;
import com.wdcg.utils.NotificationUtils;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

public class ReceiveTransitionsIntentService extends IntentService{

	public ReceiveTransitionsIntentService() {
        super("ReceiveTransitionsIntentService");
    }
	
	String id;

	@Override
	protected void onHandleIntent(Intent intent) {
		 // First check for errors
        if (LocationClient.hasError(intent)) {
            // Get the error code with a static method
            int errorCode = LocationClient.getErrorCode(intent);
            // Log the error
              
        /*
         * If there's no error, get the transition type and the IDs
         * of the geofence or geofences that triggered the transition
         */
        } else {
            // Get the type of transition (entry or exit)
            int transitionType =
                    LocationClient.getGeofenceTransition(intent);
            // Test that a valid transition was reported
            if (
                (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER)
               ) {
            	
            	List <Geofence> triggerList =
                        LocationClient.getTriggeringGeofences(intent);
           
                  id= triggerList.get(0).getRequestId();
                  
                  int dwarfId = Integer.parseInt(id);
                  
                  DwarfAccess.setDwarfGameScore(DwarfAccess.getDwarfDescriptionForDwarfId(dwarfId).getDwarf(), false, false, true);
                 
                }
            
				NotificationUtils.createNotification(getApplicationContext(), getString(R.string.notification_dwarf_title), getString(R.string.notification_dwarf_description)+" ID: "+id, R.drawable.dwarf_icon);
				
            }
        
        
	}

}
